using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player_DomainExpansionState : PlayerStateBase
{
    private float originalGravityScale;
    private Vector2 targetPosition;
    private bool isMoveTotarget;
    private bool createDomain;
    public Player_DomainExpansionState(Player player, StateMachine stateMachine, string animName) : base(player, stateMachine, animName)
    {

    }
    public override void Enter()
    {
        base.Enter();
        originalGravityScale = rb.gravityScale;
        targetPosition = CalculatetargetPosition();
        player.SetVelocity(0, player.riseSpeed);
        player.playerHealth.SetCanTakeDamage(false);
    }
    public override void Update()
    {
        base.Update();
        player.SetVelocity(0, rb.velocity.y);
        //当玩家到达目标领域点后
        if (Vector2.Distance(player.transform.position, targetPosition) <= .5f && isMoveTotarget == false)
        {
            isMoveTotarget = true;
            rb.gravityScale = 0f;
            player.SetVelocityZero();

            if (createDomain == false)
            {
                createDomain = true;
                stateTimer = skillManager.skillDomainExpansion.GetDomainDuration();
                skillManager.skillDomainExpansion.CreateDomainExpansion();
            }
        }
        if (createDomain)
        {
            skillManager.skillDomainExpansion.SpellCast();
            if (stateTimer <= 0)
            {
                isMoveTotarget = false;
                createDomain = false;
                stateMachine.ChangeState(player.idleState);
            }
        }
    }


    public override void Exit()
    {
        base.Exit();
        rb.gravityScale = originalGravityScale;
        player.playerHealth.SetCanTakeDamage(true);
        
    }
    private Vector2 CalculatetargetPosition()
    {
        RaycastHit2D targetCheck = Physics2D.Raycast(player.transform.position, Vector2.up, player.maxExpandDistance, player.whatIsGround);
        Vector2 finalDisatance = targetCheck ?
                                new Vector2(player.transform.position.x, player.transform.position.y + targetCheck.distance - 1):
        new Vector2(player.transform.position.x, player.transform.position.y + player.maxExpandDistance);
        return finalDisatance;
    }
}
